package com.djx.comm.controller;


import com.djx.comm.MessageObject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制层 父类
 * @author dingjunxiong
 */
public class BaseController {

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    public BaseController() {

    }

    public Object successMsg(String msg) {
        MessageObject object = new MessageObject();
        object.setSuccessful(true);
        object.setMessage(msg);
        return object;
    }

    public Object errorMsg(String msg) {
        MessageObject object = new MessageObject();
        object.setSuccessful(false);
        object.setMessage(msg);
        return object;
    }

    public Object successObject(Object obj) {
        MessageObject object = new MessageObject();
        object.setSuccessful(true);
        object.setData(obj);
        return object;
    }

    public Object successObject(String msg, Object obj) {
        MessageObject object = new MessageObject();
        object.setSuccessful(true);
        object.setMessage(msg);
        object.setData(obj);
        return object;
    }

    public Object errorObject(String msg) {
        MessageObject object = new MessageObject();
        object.setSuccessful(false);
        object.setMessage(msg);
        return object;
    }

    public Object errorObject(String msg, Object obj) {
        MessageObject object = new MessageObject();
        object.setSuccessful(false);
        object.setMessage(msg);
        object.setData(obj);
        return object;
    }
}
